package com.zzh.partnersys.im.session;

import io.netty.channel.Channel;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static com.zzh.partnersys.im.cache.NettyCache.ID_TEAM_MAP;


public class WSTeamSessionMemoryImpl implements WSTeamSession {

    /**
     * ����һ��teamȺ��/˽��
     * @param teamId ����(����id)
     * @param memberIDs ��Աuid
     * @return
     */
    @Override
    public Set<Long> createTeam(Long teamId, Set<Long> memberIDs) {
        return ID_TEAM_MAP.putIfAbsent(teamId, memberIDs);
    }

    /**
     * ����һ���飬��Ա�ҵ���Ȼ�����
     * @param teamId ����
     * @param uid ��Ա??
     * @return
     */
    @Override
    public Set<Long> joinMember(Long teamId, Long uid) {
        //���ж����Ƿ����
//        Group group = groupMap.get(name);
//        if (group == null){
//            return null;
//        }
        //������ʵ������map�Ƿ������key��û�л᷵��??
        return ID_TEAM_MAP.computeIfPresent(teamId, (key, value) -> {
            value.add(uid);
            return value;
        });
    }

    /**
     * �ж����Ƿ���ڣ�Ȼ��ȥ��Ⱥ�����??
     *
     * @param teamId ����
     * @param uid    ��Ա??
     * @return
     */
    @Override
    public Set<Long> removeMember(Long teamId, Long uid) {
        return ID_TEAM_MAP.computeIfPresent(teamId, (key, value) -> {
            value.remove(uid);
            return value;
        });
    }

    /**
     * ��Ⱥ������??��ɢ����)
     * @param teamId ����
     * @return
     */
    @Override
    public Set<Long> removeTeam(Long teamId) {
        return ID_TEAM_MAP.remove(teamId);
    }

    /**
     * ��ȡ�����е����г�ԱUID
     * @param teamId ����
     * @return
     */
    @Override
    public Set<Long> getMembers(Long teamId) {
        Set<Long> uidSet = ID_TEAM_MAP.get(teamId);
        if (uidSet == null){
            return new HashSet<>();
        }
        return uidSet;
    }

    /**
     * ͨ��������ȡ��Ա������member��channel
     * @param teamId ����
     * @return
     */
    @Override
    public List<Channel> getMembersChannel(Long teamId) {
//        //�Ӷ��黺�����õ�����=���õ������Աid���� todo (���ܻ���ֶ���û���һ��Ⱥ��)
//        if (!ID_TEAM_MAP.containsKey(teamId)){
//            //��ѯ����??
//            UserTeamService userTeamService = SpringUtil.getBean(UserTeamService.class);
//            Set<Long> userIdSet = userTeamService.selectTeamUserIdList(teamId);
//            //���浽��??
//            ID_TEAM_MAP.put(teamId,userIdSet);
//
//            return userIdSet.stream().map(uid -> SessionFactory.getSession().getChannel(uid)).filter(Objects::nonNull)
//                    .collect(Collectors.toList());
//        }else {
//            return ID_TEAM_MAP.get(teamId).stream()
//                    .map(uid -> SessionFactory.getSession().getChannel(uid))
//                    .filter(Objects::nonNull)
//                    .collect(Collectors.toList());
//        }
        return ID_TEAM_MAP.get(teamId).stream()
                    .map(uid -> SessionFactory.getSession().getChannel(uid))
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
    }
}
